<?php

namespace app\controller;

use app\BaseController;
use app\message;
use app\Request;
use app\service\System;
use app\stateData;
use think\facade\Cache;

class UserMessage extends BaseController
{
    /*******************
     * 消息列表
     */

    //获取消息列表
    public function getMessageList(Request $request,\app\model\UserMessage $message)
    {
        $type = $request->get ('type')?:'';
        $delay = $request->get ('delay')?:1;
        $page = $request->get ('page')?:1;
        $pageNum = $request->get ('pageNum')?:stateData::$_page;
//        if ($type == 'y'){
//            $type = '';
//        }
        $result = $message->getTypeMessageListModel ($type, $request->param ('user_login_id'),$page,$pageNum,$delay);

        $result['pageNumber'] = $message->getTypeMessageCountModel($type, $request->param ('user_login_id'),$delay); //全部消息

        $result['unread'] = $message->getTypeMessageUnreadCountModel($type, $request->param ('user_login_id'),$delay);//未读消息

        $pages['total'] = $message->getTypeMessageCountModel($type, $request->param ('user_login_id'),$delay);
        $pages['pageNum'] = $pageNum;

        return ReJson (message::$SUCC_CODE,message::$SUCC_SELECT_DATA,$result,$pages);
    }

    //全部标记已读
    public function setMessageMark(Request $request,\app\model\UserMessage $message)
    {
        $message->setUidMessageMark($request->param ('user_login_id'));

        return ReJson (message::$SUCC_CODE,message::$SUCC_SETTING);
    }


    //删除消息
    public function delMessage(Request $request,\app\model\UserMessage $message,System $system)
    {
        $id = $request->post ('id');

        if (empty($id)) return ReJson (message::$ERROR_CODE,message::$ERR_PARAM_EMPTY);

        $message->delMessage($system->analysisId ($id));

        return ReJson (message::$SUCC_CODE,message::$SUCC_DATELE_DATA);

    }

    //消息详情
    public function getMessageDetails(Request $request,\app\model\UserMessage $message)
    {
        $id = $request->get ('id');

        if (empty($id)) return ReJson (message::$ERROR_CODE,message::$ERR_PARAM_EMPTY);

        $data = $message->getIdFindModel($id);

        return ReJson (message::$SUCC_CODE,message::$SUCC_SELECT_DATA,$data);
    }


    //消息延后处理
    public function setMessLater(Request $request,\app\model\UserMessage $message)
    {
        $param = $request->post ();

        if (empty($param['id']) || empty($param['hour'])) return ReJson (message::$ERROR_CODE,message::$ERR_PARAM_EMPTY);

        $find = $message->getIdFindModel($param['id']);

        if (empty($find)) return ReJson (message::$ERROR_CODE,message::$MESSAGE_EMPTY);

        $date = date('Y-m-d,H:i:s', strtotime ("+".$param['hour']." hour", strtotime($find['execution_date'])));

        $message->setMessLaterModel($param['id'],$date);

        return ReJson (message::$SUCC_CODE,message::$SUCC_OPERATE);
    }

    //添加未收款原因
    public function getUncollectedReason(Request $request,\app\model\UserMessage $message)
    {
        $param = $request->post ();

        if (empty($param['id'])) return ReJson (message::$ERROR_CODE,message::$ERR_PARAM_EMPTY);

        $message->setFeedbackReasonModel($param['id'],$param['feedback_reason']);

        return ReJson (message::$SUCC_CODE,message::$SUCC_INSERT_DATA);
    }


    public function readMsg(Request $request,\app\model\UserMessage $message)
    {
        $id =  $request->post ('id');
        if (empty($id)) return ReJson (message::$ERROR_CODE,message::$ERR_PARAM_EMPTY);
        $message->editReadStatus([$id]);
        return ReJson (message::$SUCC_CODE,'');
    }

}